#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const LL MAXN=1e6+7;
LL prime[MAXN/2], p_cnt;
bool is_prime[MAXN];
void Init() {
	memset(is_prime, 1, sizeof(is_prime));
	p_cnt=0;
	for (LL i=2; i<MAXN; ++i) {
		if (is_prime[i]) {
			prime[p_cnt++]=i;
		}
		for (LL j=0; j<p_cnt&&i*prime[j]<MAXN; ++j) {
			if (i%prime[j]==0) {
				is_prime[i*prime[j]]=false;
				break;
			}
			else {
				is_prime[i*prime[j]]=false;
			}
		}
	}
}
vector<LL> yyzz;
void min_yinzi(LL ask) {
	for (LL i=0; i<p_cnt&&prime[i]<=ask; ++i) {
		if (ask%prime[i]==0) {
			yyzz.push_back(prime[i]);
			while (ask%prime[i]==0) ask=ask/prime[i];
		}
	}
	if (ask>1) yyzz.push_back(ask);
}
LL a[MAXN];
LL GCD(LL a1, LL b1) {
	return a1%b1==0? b1 : GCD(b1, a1%b1);
}
bool cmp(LL a, LL b) {
	return a>b;
}
int main() {
	cin.sync_with_stdio(false);
	Init();
	LL T;
	cin>>T;
	LL n;
	LL cnt;
	while (T--) {
		cin>>n;
		cnt=0;
		LL zz=66;
		bool first=true;
		for (LL i=0; i<n; ++i) {
			cin>>a[i];
			cnt+=a[i];
			if (a[i]==0) continue;
			else if (first) zz=a[i],first=false;
			else {
				zz=GCD(zz, a[i]);
			}
		}
		if (zz>1) cout<<0<<endl;
		else {
			yyzz.clear();
			min_yinzi(cnt);
			LL len=yyzz.size();
			LL ans=0x3f3f3f3f3f3f3f3f;
			for (int j=0; j<len; ++j) {
				LL yinzi=yyzz[j];
//				cout<<"yinzi: "<<yinzi<<endl;
				LL res=0;
				LL cnt=0; 
				vector<LL> vec;
				for (LL i=0; i<n; ++i) {
					if (a[i]==0) continue;
					if (a[i]%yinzi==0) continue;
					LL t=a[i]%yinzi;
					cnt+=t;
					vec.push_back(t);
				}
				sort(vec.begin(), vec.end(), cmp);
				LL lenc=cnt/yinzi;
				for (int i=0; i<lenc; ++i) res+=(yinzi-vec[i]);
				if (res<ans) ans=res;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}
